package com.cade.agriculturemall.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.IBinder;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.cade.agriculturemall.R;


/**
 * User: Tom
 * Date: 2016-05-25
 * Time: 11:11
 * FIXME
 */
public class SearchEditText extends FrameLayout implements View.OnFocusChangeListener, View.OnClickListener, TextView.OnEditorActionListener {

    private CleanEditText editText;
    private TextView imageButton;
    private OnSearchActionListener onSearchActionListener;


    public SearchEditText(Context context) {
        super(context);
        install();
    }

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        install();
    }

    public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        install();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        install();
    }

    private void install() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_search_edit_text, this, true);
        if (!isInEditMode()) {
            editText = (CleanEditText) findViewById(R.id.edit_query);
            imageButton = (TextView) findViewById(R.id.btn_search);
            editText.setOnFocusChangeListener(this);
            imageButton.setOnClickListener(this);
            editText.setOnEditorActionListener(this);
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (onSearchActionListener != null) {
                        onSearchActionListener.onSearchTextChanged(editText.getText().toString());
                    }
                }
            });
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {

    }

    @Override
    public void onClick(View v) {
        if (onSearchActionListener != null) {
            onSearchActionListener.onSearchAction(editText.getText().toString());
        }
        hideKeyboard(v.getWindowToken());
    }

    public OnSearchActionListener getOnSearchActionListener() {
        return onSearchActionListener;
    }

    public void setOnSearchActionListener(OnSearchActionListener onSearchActionListener) {
        this.onSearchActionListener = onSearchActionListener;
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (onSearchActionListener != null) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH)

                onSearchActionListener.onSearchAction(editText.getText().toString());
            hideKeyboard(v.getWindowToken());
            return true;


        }

        return false;
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public CleanEditText getEditText() {
        return editText;
    }

    public interface OnSearchActionListener {
        void onSearchAction(String keyword);

        void onSearchTextChanged(String keyword);

    }

    public void setHint(String hint) {
        editText.setHint(hint);
    }

    public String getText() {
        return editText.getText().toString();
    }

    public void setText(String text) {
        editText.setText(text);
    }
}